package com.gold.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gold.common.JsonUtils;
import com.gold.common.StringUtils;
import com.gold.entity.CardInfo;
import com.gold.service.CardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author XiaoHao
 * @date 2022-05-11 09:04
 */
@Controller
@RequestMapping("/card")
public class CardInfoController {
    @Autowired
    CardInfoService cardInfoService;


    @PreAuthorize("hasAnyRole('admin')")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public String save(@RequestBody CardInfo cardInfo) {
        try {
            cardInfoService.insert(cardInfo);
        } catch (Exception e) {
            return JsonUtils.responseJson(false, 400, "新增错误：" + e.getMessage(), "").toJSONString();
        }
        return JsonUtils.responseJson(true, 200, "新增成功", "").toJSONString();
    }
    @PreAuthorize("hasAnyRole('admin')")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@RequestBody CardInfo cardInfo) {
        try {
            cardInfoService.delete(cardInfo);
        } catch (Exception e) {
            return JsonUtils.responseJson(false, 400, "删除错误：" + e.getMessage(), "").toJSONString();
        }
        return JsonUtils.responseJson(true, 200, "删除成功", "").toJSONString();
    }

    @PreAuthorize("hasAnyRole('admin')") //校检角色
    @RequestMapping(value = "getList", method = RequestMethod.POST)
    @ResponseBody
    public String getList(@RequestBody JSONObject parm, HttpServletRequest request) {
        int page = 1;
        int size = 10;
        CardInfo cardInfo = new CardInfo();
        if (parm != null) {
            String page1 = parm.getString("page");
            String size1 = parm.getString("size");
            if (StringUtils.isNotEmpty(page1)) {
                page = Integer.parseInt(page1);
            }
            if (StringUtils.isNotEmpty(size1)) {
                size = Integer.parseInt(size1);
            }
            cardInfo.setType(parm.getString("type"));
            cardInfo.setStatus(parm.getString("status"));
        }
        IPage<CardInfo> cardInfos = cardInfoService.selectAll(page, size, cardInfo);
        if (cardInfos == null) {
            return JsonUtils.responseJson(true, 200, "卡密数据", "").toJSONString();
        }
        return JsonUtils.responseJson(true, 200, "卡密数据", (JSONObject) JSONObject.toJSON(cardInfos)).toJSONString();
    }
}
